;
(function() {

	var skill = {
		_getTicketFactoryIdList: function() {
			var ticket_factory_list = this.ticket_factory_list;
			if (!Array.isArray(ticket_factory_list)) {
				ticket_factory_list = this.ticket_factory_list = [];
				this.save();
			}
			return ticket_factory_list;
		},
		_getTicketFactory: function(ticket_factory_id) {
			var ticket_factory_list = this._getTicketFactoryIdList();
			if (ticket_factory_list.indexOf(ticket_factory_id) === -1) {
				throwCusError("优惠券不属于指定商家")
			}
			var ticket_factory = TicketFactoryCon.get(ticket_factory_id);
			if (!ticket_factory) {
				throwCusError("找不到指定优惠券")
			}
			return ticket_factory;
		},
		getTicketFactoryList: function(num, page) {
			var ticket_factory_list = this._getTicketFactoryIdList();
			ticket_factory_list = data_cut.page(ticket_factory_list, num, page);
			ticket_factory_list = ticket_factory_list.map(function(ticket_factory_id) {
				return TicketFactoryCon.get(ticket_factory_id);
			});
			return ticket_factory_list;
		},
		addTicketFactory: function(ticket_factory_info) {
			var ticket_factory_list = this._getTicketFactoryIdList();
			if (ticket_factory_list.length >= config.busTicketFactoryNumber) {
				throwCusError("商家同时的拥有优惠券数量已经达到上限")
			}
			ticket_factory_info.bus_id = this._id;
			var ticket_factory = new TicketFactoryCon(ticket_factory_info);
			var ticket_factory_id = ticket_factory._id
			ticket_factory_list.unshift(ticket_factory_id);
			this.save();
			return ticket_factory;
		},
		//优惠券暂时不给予修改功能，后面可能会加上是否发布参数，在发布之前还能做一定修改
		updateTicketFactory: function() {},
		removeTicketFactory: function(ticket_factory_id) {
			var ticket_factory = this._getTicketFactory(ticket_factory_id);
			this.ticket_factory_list = $Array.remove(this.ticket_factory_list, ticket_factory._id);
			ticket_factory.destroy();
			this.save();
		},
		sendTicketFactoryToUserTicket: function(user_id, ticket_factory_id) {
			var user = UserCon.get(user_id);
			var ticket_factory = this._getTicketFactory(ticket_factory_id);
			var ticket = ticket_factory.toTicket(this._id);
			user._getTicketIdList().unshift(ticket._id);
			user.save();
			return ticket;
		}
	};
	module.exports = skill;
	var db = require("../db/index");
	var TicketFactoryCon = require("../ticket_factory/_con");
	var DomainCon = require("../domain/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
	var tld = require('tldjs');
	var fs = require("fs");
	var Fiber = require('fibers');
}());